layui.config({
    base: '/layuiadmin/' //静态资源所在路径
}).extend({
    index: 'lib/index' //主入口模块
}).use(['index', 'table'], function () {
    var table = layui.table
        , form = layui.form;

    //监听查询框搜索
    form.on('submit(LAY-app-contlist-search)', function (data) {
        var field = data.field;
        layer.msg(JSON.stringify(data))
        //执行重载
        table.reload('LAY-app-content-list', {
            where: field
        });
    });
    //数据表格顶部工具栏
    var $ = layui.$, active = {
        batchdel: function () {
            var checkStatus = table.checkStatus('LAY-app-content-list')
                , checkData = checkStatus.data; //得到选中的数据

            if (checkData.length === 0) {
                return layer.msg('请选择数据');
            }

            layer.confirm('确定删除吗？', function (index) {

                //执行 Ajax 后重载
                /*
                admin.req({
                  url: 'xxx'
                  //,……
                });
                */
                table.reload('LAY-app-content-list');
                layer.msg('已删除');
            });
        },
        add: function () {
            layer.open({
                type: 2
                , title: '添加文章'
                , content: 'listform.html'
                , maxmin: true
                , area: ['550px', '550px']
                , btn: ['确定', '取消']
                , yes: function (index, layero) {
                    //点击确认触发 iframe 内容中的按钮提交
                    var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                    submit.click();
                }
            });
        }
    };

    $('.layui-btn.layuiadmin-btn-list').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    //渲染数据表格
    table.render({
        elem: '#LAY-app-content-list'
        , size: 'sm'
        , method: 'post'
        , url: '/admin/sysJob/list'
        , page: true
        , cols: [[
            {type: 'numbers', fixed: "left"},
            {type: 'checkbox', fixed: 'left'},
            {field: 'id', title: '主键'},
            {field: 'jobName', title: '任务名称'},
            {field: 'jobGroup', title: '任务组名'},
            {field: 'invokeTarget', title: '调用目标字符串'},
            {field: 'cronExpression', title: 'cron执行表达式'},
            {field: 'misfirePolicyStr', title: '计划执行错误策略'},
            {field: 'concurrentStr', title: '是否并发执行'},
            {field: 'createTime', title: '创建时间'},
            {field: 'remark', title: '备注信息'},
            {field: 'status', title: '状态', templet: '#buttonTpl'},
            {title: '操作', align: 'center', fixed: 'right', toolbar: '#table-content-list'}
        ]]
    });

    //监听数据表格状态栏
    form.on('switch(status)', function (obj) {
        layer.tips(this.value + ' ' + this.name + '：' + obj.elem.checked, obj.othis);
        // $.ajax({
        //     type: "post",
        //     url: "/admin/sysUser/updateStatus",
        //     data: {'id': obj.value,"status":status},
        //     dataType: "json",
        //     success: function (result) {
        //         if (result.success == true) {
        //             layer.msg(result.msg, {
        //                 icon: 6,
        //                 offset: '150px',
        //                 time: 300
        //             }, function () {
        //                 table.reload('dataGrid', {
        //                     where: $('#searchForm').serializeJSON()
        //                 });
        //             });
        //         } else {
        //             layer.msg(result.msg, {
        //                 icon: 5,
        //                 offset: '150px'
        //             });
        //         }
        //     }
        // });
    });

    //监听数据表格工具条
    table.on('tool(LAY-app-content-list)', function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            layer.confirm('确定删除此文章？', function (index) {
                obj.del();
                layer.close(index);
            });
        } else if (obj.event === 'edit') {
            layer.open({
                type: 2
                , title: '编辑文章'
                , content: '../../../views/app/content/listform.html?id=' + data.id
                , maxmin: true
                , area: ['550px', '550px']
                , btn: ['确定', '取消']
                , yes: function (index, layero) {
                    var iframeWindow = window['layui-layer-iframe' + index]
                        , submit = layero.find('iframe').contents().find("#layuiadmin-app-form-edit");

                    //监听提交
                    iframeWindow.layui.form.on('submit(layuiadmin-app-form-edit)', function (data) {
                        var field = data.field; //获取提交的字段

                        //提交 Ajax 成功后，静态更新表格中的数据
                        //$.ajax({});
                        obj.update({
                            label: field.label
                            , title: field.title
                            , author: field.author
                            , status: field.status
                        }); //数据更新

                        form.render();
                        layer.close(index); //关闭弹层
                    });

                    submit.trigger('click');
                }
            });
        }
    });
});